/**
	This file is part of MemoryTrace.

	MemoryTrace is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

        Contributed by Ameziane NAIT ABDELAZIZ.
        
	MemoryTrace is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with MemoryTrace.  If not, see <http://www.gnu.org/licenses/>.
**/



/* 
 * File:   message_passing.cpp
 * Author: maxds
 *
 * Created on 31 juillet 2011, 18:20
 */

#include <cstdlib>
#include <sys/msg.h>
#include <stddef.h>
#include <unistd.h>


using namespace std;


/**
 *
 */
#include <cassert>
#include <cstdlib>
#include <cstdio>
#include "ipc.h"


int main(int argc, char** argv) {

    
    pid_t child=fork();
    
    MESSAGE_QUEUE::initQueues();
    
    
    
    if(!child){
        for(int i=0;i<10000;i++){
            MESSAGE_QUEUE::sendMessageToConsumer(MSG_TAG_PLEIN);
            
            //on bloque jusque a nouvel ordre du consommateur
            MESSAGE_QUEUE::receiveMessageFromConsumer(MSG_TAG_RECU);
            printf("Ok le message est recu\n");
            
        }
        
        char c;
        
        MESSAGE_QUEUE::sendMessageToConsumer(MSG_TAG_QUIT);
        
        
        printf("bye from father\n");
        return 0;
    }
        
    if(child){
        bool cnt=true;
        while(cnt){
            
            long typeMsg=MESSAGE_QUEUE::receiveMessageFromProducer(MSG_TAG_ALL);
            
            switch (typeMsg){
                case MSG_TAG_QUIT:
                    printf("Le fils recoit le message quitte\n");
                    cnt=false;
                    break;
                    
                    
                case MSG_TAG_PLEIN:
                    
                    MESSAGE_QUEUE::sendMessageToProducer(MSG_TAG_RECU);
                    
                    break;
                default:
                    break;
                        
                    
            }
        }
        
        MESSAGE_QUEUE::destroyQueues();
        printf("bye from child\n");
        return 0;
    }
    
    return 0;
}

